<?php
namespace gzapi\department\controller;

use gzapi\common\business\Department;
use cmf\controller\PublicController;
use think\Validate;

class ServiceWorkController extends PublicController
{
    //方法验证参数
    protected $auth = '*';
    protected $DepartmentCompany;
    protected function initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10001, 'msg' => '登录已失效!']);
        }
        $this->DepartmentCompany = new Department;
    }

    //获取工程数量
    public function projectNum()
    {
        $where['xj_dw'] = ['=',$this->user['server_code']];
        $where['xj_xjr'] = ['=',$this->user['engineer_code']];
        $xjzt = input('param.xjzt');
        if($xjzt)
        {
            $where['xj_xjzt'] = ['in',$xjzt];
        }
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        //获取安装数量
        $data['az'] = $this->DepartmentCompany->serviceAzdNum($where);
        $this->success("查询成功!", ['data' => $data]);
    }
//查询单个项目信息
    public function projectInfo(){
        $where = input('param.');
        $res=Db::table('cmf_gz_hise_service_project')->where($where)->find();
        $this->success("查询成功!", ['data' => $res]);
    }
    //查看工程列表
    public function projectList()
    {
        $where = [];
        $page = input('param.page');
        $limit = input('param.limit');
        $where['xj_dw'] = ['=',$this->user['server_code']];
        $where['xj_xjr'] = ['=',$this->user['engineer_code']];
        $gczt = input('param.type');
        if(!$gczt)
        {
            $where['gczt'] = ['in',[6,7]];
        }else{
            $where['gczt'] = ['in',[5]];
        }
        $followWhere['bm_code'] = ['=',$this->user['server_code']];
        $followWhere['mobilphone'] = ['=',$this->user['phone']];
        $selectdata = $this->request->param();
        $selectdata['xj_dw'] = $this->user['server_code'];
        $selectdata['xj_xjr'] = $this->user['phone'];
        $selectazd = $this->DepartmentCompany->selectazd($selectdata);
        $where = array_merge($where,$selectazd);
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        //获取本状态安装单
        $data = $this->DepartmentCompany->serviceAzdList($where,"*",$page,$limit,$followWhere);
        $this->success("查询成功!", ['data' => $data]);
    }

    //获取关注列表数量
    public function followListNum()
    {
        $data = input('param.');
        if(isset($data['is_strategic']))
        {
            $followWhere['is_strategic'] = $data['is_strategic'];
        }
        $followWhere['bm_code'] = ['=',$this->user['server_code']];
        $followWhere['mobilphone'] = ['=',$this->user['engineer_code']];
        $data = input('param.');
        if(isset($data['is_strategic']))
        {
            $followWhere['is_strategic'] = $data['is_strategic'];
        }
        //获取关注数量
        $azd_ids = $this->DepartmentCompany->followCount($followWhere);
        $this->success("查询成功!", ['data' => $azd_ids['num']]);
    }


    //进入关注列表
    public function followList()
    {
        $type = input('param.type');
        $follow = 1;
        $where = [];
        if($type)
        {
            $where['gczt'] = ['=',$type];
        }
        $page = input('param.page');
        $limit = input('param.limit');
        $where['xj_dw'] = ['=',$this->user['server_code']];
        $xjzt = input('param.xjzt');
        if($xjzt)
        {
            $where['xj_xjzt'] = ['in',$xjzt];
        }
        $followWhere['bm_code'] = ['=',$this->user['server_code']];
        $followWhere['mobilphone'] = ['=',$this->user['engineer_code']];
        //获取关注的安装单
        $azd_ids = $this->DepartmentCompany->followList($followWhere);
        $where['azd_id'] = ['in' , $azd_ids['follow']];
        //获取本状态关注安装但
        $followWhere['bm_code'] = ['=',$this->user['server_code']];
        $followWhere['mobilphone'] = ['=',$this->user['phone']];
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        //获取本状态安装单
        $data = $this->DepartmentCompany->serviceAzdList($where,"*",$page,$limit,$followWhere);
        $this->success("查询成功!", ['data' => $data]);
    }

    //获取未完成的巡检安装单数量
    public function inspectListNumIng()
    {
        $where['a.xj_dw'] = ['=',$this->user['server_code']];
        $where['a.xj_xjr'] = ['=',$this->user['engineer_code']];
        $where['a.xj_xjzt'] = ['in','0,1,2,3,4'];
        $xj_xjzt = input('param.xjzt');
        if($xj_xjzt)
        {
            $inswhere['xj_xjzt'] = $xj_xjzt;
        }
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'] == 1)
            {
                $config=config('Gzflag');
                $where['w.ywbk'] = ['in',$config];
            }else{
                $config=config('Gzflag');
                $where['w.ywbk'] = ['not in',$config];
            }
           
        }
        $data = $this->DepartmentCompany->inspectListNumIng($where);
        $this->success("查询成功!", ['data' => $data]);
    }

    //进入未完成的巡检列表
    public function inspectList()
    {
        $inswhere['xj_xjzt'] = ['in','0,1,2,3,4'];
        $inswhere['xj_dw'] = ['=',$this->user['server_code']];
        $inswhere['xj_xjr'] = ['=',$this->user['engineer_code']];
        $xj_xjzt = input('param.xjzt');
        if($xj_xjzt)
        {
            $inswhere['xj_xjzt'] = $xj_xjzt;
        }
        $inspect = $this->DepartmentCompany->inspectListstr($inswhere);
        $where = [];
        $page = input('param.page');
        $limit = input('param.limit');
//        $where['fwfgs'] = ['=',$this->user['server_code']];
        $where['azd_id'] = ['in',$inspect];
        //获取本状态安装单
        $followWhere['bm_code'] = ['=',$this->user['server_code']];
        $followWhere['mobilphone'] = ['=',$this->user['phone']];
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        $inspectionWhere['xj_dw'] = ['=',$this->user['server_code']];
        $inspectionWhere['xj_xjr'] = ['=',$this->user['phone']];
        $data = $this->DepartmentCompany->projectList($where,"*",$page,$limit,$inspectionWhere,$followWhere);
        $this->success("查询成功!", ['data' => $data]);
    }

    //取消关注
    public function followOff()
    {
        $azd_id = input('param.azd_id');
        $where = [];
        $where['azd_id'] = ['=',$azd_id];
        $where['bm_code'] = ['=',$this->user['server_code']];
        $where['mobilphone'] = ['=',$this->user['phone']];
        $result = $this->DepartmentCompany->followOffOn(2,$where);
        if($result)
        {
            $this->success("取消关注成功!");
        }else{
            $this->error('取消关注失败');
        }
    }

    //点击关注
    public function followOn()
    {
        $azd_id = input('param.azd_id');
        $where = [];
        $where['azd_id'] = ['=',$azd_id];
        $where['bm_code'] = ['=',$this->user['server_code']];
        $where['mobilphone'] = ['=',$this->user['phone']];
        $data['azd_id'] = $azd_id;
        $data['bm_code'] = $this->user['server_code'];
        $data['mobilphone'] = $this->user['phone'];
        $result = $this->DepartmentCompany->followOffOn(1,$where,$data);
        if($result)
        {
            $this->success("添加关注成功!");
        }else{
            $this->error('添加关注失败');
        }
    }













}
